package com.gomtel.smartdevice.ui.activity;

import android.content.Intent;
import android.support.v7.widget.AppCompatCheckBox;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.api.MessageManager;
import com.gomtel.smartdevice.base.WatchAppBaseActivity;
import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.moudle.db.entity.LoginUserInfo;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.ui.dialog.ShowDialog;
import com.gomtel.smartdevice.ui.view.CircleImageView;
import com.gomtel.smartdevice.utils.SPUtil;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/18 0018.
 */

public class SettingActivity extends WatchAppBaseActivity implements View.OnClickListener {

    @BindView(R.id.img_btn_black)
    ImageButton img_btn_black;
    @BindView(R.id.photo_view)
    CircleImageView photo_view;
    @BindView(R.id.title_layout)
    RelativeLayout title_layout;
    @BindView(R.id.line)
    View line;
    @BindView(R.id.layout_set_a_goal)
    RelativeLayout layout_set_a_goal;
    @BindView(R.id.layout_change_password)
    RelativeLayout layout_change_password;
    @BindView(R.id.layout_about_)
    RelativeLayout layout_about_;
    @BindView(R.id.layout_logout)
    RelativeLayout layout_logout;
    @BindView(R.id.cb_objective)
    AppCompatCheckBox cb_objective;
    @BindView(R.id.cb_call)
    AppCompatCheckBox cb_call;
    @BindView(R.id.cb_wearing)
    AppCompatCheckBox cb_wearing;
    @BindView(R.id.cb_sedentary)
    AppCompatCheckBox cb_sedentary;
    private ShowDialog showDialog;
    private LoginUserInfo loginUserInfo;


    @Override
    protected void registerEvent(CommonEvent commonevent) {
        switch (commonevent.getCode()) {
            case EventCode.LOGIN_OUT:
                startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                finish();
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initClass() {
        showDialog = ShowDialog.newInstance();
    }

    @Override
    protected void initData() {
        loginUserInfo = dataManager.queryLoginUserInfo(UserContent.UserName);
    }

    @Override
    protected void initView() {
        cb_objective.setChecked((boolean) SPUtil.get(this, "cb_objective", false));
        cb_call.setChecked((boolean) SPUtil.get(this, "cb_call", false));
        cb_wearing.setChecked((boolean) SPUtil.get(this, "cb_wearing", false));
        cb_sedentary.setChecked((boolean) SPUtil.get(this, "cb_sedentary", false));
        line.setVisibility(View.GONE);
        title_layout.setBackground(getResources().getDrawable(R.color.color_blue_));
        Glide.with(this).load(loginUserInfo.getUserPhoto()).into(photo_view);
    }

    @Override
    protected void initListener() {
        img_btn_black.setOnClickListener(this);
        photo_view.setOnClickListener(this);
        layout_set_a_goal.setOnClickListener(this);
        layout_change_password.setOnClickListener(this);
        layout_about_.setOnClickListener(this);
        layout_logout.setOnClickListener(this);
        cb_objective.setOnClickListener(this);
        cb_call.setOnClickListener(this);
        cb_wearing.setOnClickListener(this);
        cb_sedentary.setOnClickListener(this);
        initToolsCheck();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_btn_black:
                finish();
                break;
            case R.id.photo_view:
                startActivity(new Intent(SettingActivity.this, PersonageActivtiy.class));
                break;
            case R.id.layout_set_a_goal:
                Intent intent = new Intent(SettingActivity.this, MultiTypeActivity.class);
                intent.putExtra("ActionType", String.valueOf(EventCode.SET_A_GOAL));
                startActivity(intent);
                break;
            case R.id.layout_change_password:
                startActivity(new Intent(SettingActivity.this, ChagePassWordActivity.class));
                break;
            case R.id.layout_about_:
                Intent intentType = new Intent(SettingActivity.this, MultiTypeActivity.class);
                intentType.putExtra("ActionType", String.valueOf(EventCode.ABOUT_));
                startActivity(intentType);
                break;
            case R.id.layout_logout:
                showDialog.showStatusDialog(this, EventCode.LOGIN_OUT);
                break;

        }
    }

    private void initToolsCheck() {
        cb_objective.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SPUtil.put(this, "cb_objective", isChecked);
            MessageManager.getInstance().sendNotification(MessageManager.NOTIFICATION_ID_GOAL_ACHIEVED,
                    isChecked ? MessageManager.NOTIFICATION_TURN_ON : MessageManager.NOTIFICATION_TURN_OFF);
        });

        cb_call.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SPUtil.put(this, "cb_call", isChecked);
            MessageManager.getInstance().sendNotification(MessageManager.NOTIFICATION_ID_PHONE_RANG,
                    isChecked ? MessageManager.NOTIFICATION_TURN_ON : MessageManager.NOTIFICATION_TURN_OFF);
        });

        cb_sedentary.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SPUtil.put(this, "cb_sedentary", isChecked);
            MessageManager.getInstance().sendNotification(MessageManager.NOTIFICATION_ID_SEDENTARY,
                    isChecked ? MessageManager.NOTIFICATION_TURN_ON : MessageManager.NOTIFICATION_TURN_OFF);
        });

        cb_wearing.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SPUtil.put(this, "cb_wearing", isChecked);
            MessageManager.getInstance().sendNotification(MessageManager.NOTIFICATION_ID_WEARING_LOOSE,
                    isChecked ? MessageManager.NOTIFICATION_TURN_ON : MessageManager.NOTIFICATION_TURN_OFF);
        });

    }


}
